package demo.bytebuddy;

import net.bytebuddy.ByteBuddy;
import net.bytebuddy.implementation.FixedValue;
import net.bytebuddy.matcher.ElementMatchers;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;

/**
 * description: 使用bytebuddy创建一个类<br>
 * createTime: 2019/2/199:32 <br>
 *
 * @author zzh
 */
public class ByteBuddyDemo1 {

    public static void main(String[] args) throws IllegalAccessException, InstantiationException {
        Class<?> dynamicType = new ByteBuddy()
                .subclass(Object.class)
                .method(ElementMatchers.named("toString"))
                .intercept(FixedValue.value("Hello World!"))
                .make()
                .load(ByteBuddyDemo1.class.getClassLoader())
                .getLoaded();
        assertThat(dynamicType.newInstance().toString(), is("Hello World!"));
        System.out.println(dynamicType.newInstance().toString());
        System.out.println(dynamicType.newInstance().getClass().getName());
    }
}